package cn.sqlog.simplewol.controller;


import cn.sqlog.simplewol.base.BaseController;
import cn.sqlog.simplewol.base.BaseSelectSymbol;
import cn.sqlog.simplewol.base.ResponseBean;
import cn.sqlog.simplewol.base.entity.SymbolPlus;
import cn.sqlog.simplewol.entity.SqWol;
import cn.sqlog.simplewol.service.ISqWolService;
import cn.sqlog.simplewol.wol.WolUtils;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author SQ
 * @since 2019-09-02
 */
@RestController
@RequestMapping(value = "/sq_wol",method = RequestMethod.POST)
public class SqWolController extends BaseController<SqWol, ISqWolService> {
    @Autowired
    private ISqWolService sqWolService;
    @RequestMapping("/save")
    public ResponseBean save(@RequestBody SqWol sqWol){
        boolean add = super.add(sqWol);
        return new ResponseBean(0, "Success", add);
    }

    @RequestMapping("/get_list_by_page")
    public ResponseBean get_list_by_page(@RequestBody Map<String,String> map){
        SymbolPlus symbolPlus = BaseSelectSymbol.select_Analysis(map);
        IPage<SqWol> SqWolIPage = super.base_get_list_by_page(symbolPlus);
        return new ResponseBean(0, "Success", SqWolIPage);
    }

    @RequestMapping("/delete_by_id")
    public ResponseBean delete_by_id(@RequestBody SqWol sqWol){
        boolean del = super.del(sqWol.getWolId());
        return new ResponseBean(0, "Success", del);
    }

    @RequestMapping("/update_by_id")
    public ResponseBean update_by_id(@RequestBody SqWol sqWol){
        boolean update = super.update(sqWol);
        return new ResponseBean(0, "Success", update);
    }

    @RequestMapping("/update")
    public ResponseBean update(@RequestBody Map<String,String> map){
        UpdateWrapper updateWrapper = BaseSelectSymbol.update_Analysis(map);
        boolean update = super.update(updateWrapper);
        return new ResponseBean(0, "Success", update);
    }

    /**
     * 开机  mapyongin
     * @param map
     * @return
     */
    @RequestMapping("/startBoot")
    public ResponseBean startBoot(@RequestBody Map<String,String> map){
        SymbolPlus symbolPlus = BaseSelectSymbol.select_Analysis(map);
        List<SqWol> list = sqWolService.list(symbolPlus.getQueryWrapper());
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        for (SqWol sqWol : list) {
            boolean b = WolUtils.sendMagicPackage(sqWol);
            if (!b){
                stringStringHashMap.put(sqWol.getWolIp(),sqWol.getWolMac()) ;
            }

        }
        if (stringStringHashMap.size()>0) {
            return  new ResponseBean (2,"Error Start",stringStringHashMap);

        }else{
            return  new ResponseBean (0,"Success",stringStringHashMap);
        }

    }


}
